package com.xjscrm.server.service.contackclean.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.mobile.collect.api.dto.MicaTransDto;
import com.uzai.mobile.collect.api.dto.msg.MicaContactCleanMsg;
import com.uzai.trace.TraceGenerate;
import com.xjscrm.common.entity.SyncCustomerJob;
import com.xjscrm.common.entity.SyncGroupJob;
import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.server.common.util.DeviceTools;
import com.xjscrm.server.mapper.SyncCustomerJobMapper;
import com.xjscrm.server.mapper.SyncGroupJobMapper;
import com.xjscrm.server.mapper.WwDeviceMapper;
import com.xjscrm.server.service.contackclean.ContactCleanService;
import com.xjscrm.server.vo.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 同步通讯录和群完成指令服务实现类
 *
 * @author makejava
 * @since 2024-03-01 17:01:12
 */
@Service
@Slf4j
public class ContactCleanServiceImpl implements ContactCleanService {
    @Autowired
    private WwDeviceMapper wwDeviceMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SyncCustomerJobMapper syncCustomerJobMapper;
    @Autowired
    private SyncGroupJobMapper syncGroupJobMapper;

    /**
     * 处理同步通讯录和群完成指令
     *
     * @param micaTransDto 上线实体类
     * @return 实例对象
     */
    @Async("taskExecutorForEvent")
    @TraceGenerate //注入消息id标签
    public void contactClean(MicaTransDto micaTransDto) {

        try {
            log.info("--监控系统日志--开始--");

            if (StringUtils.isBlank(micaTransDto.getDeviceId())) {
                return;
            }

            log.info("--监控系统日志--查询机器人--");
            WwDevice wwDevice = wwDeviceMapper.queryByDeviceId(micaTransDto.getDeviceId(), micaTransDto.getMerId());
            if (wwDevice == null) {
                return;
            }
            Device device = DeviceTools.transDevice(wwDevice);
            if (device == null) {
                return;
            }

            MicaContactCleanMsg micaContactCleanMsg = JSONObject.parseObject(micaTransDto.getData().toString(), MicaContactCleanMsg.class);
            if (micaContactCleanMsg == null) {
                return;
            }

            int now = DateUtil.getTime();

            // 0 好友，1群
            Integer userType = micaContactCleanMsg.getUserType();
            if (userType == null) {
                return;
            }

            //更新通讯录完成
            if (userType.intValue() == 0) {
                //查询更新通讯录表中该机器人进行中的记录,修改成完成状态
                SyncCustomerJob syncCustomerJob = syncCustomerJobMapper.selectByDeviceUniqueIdAndStatus(device.getDeviceUniqueId(), 1);
                if (syncCustomerJob != null) {
                    syncCustomerJob.setStatus(2);
                    syncCustomerJob.setUpdateTime(now);
                    syncCustomerJobMapper.updateByIdSelective(syncCustomerJob);

                    log.info("--监控系统日志--更新通讯录状态--");

                }
            } else if(userType.intValue() == 1){ //更新群
                //查询更新群成员表中该机器人进行中的记录,修改成完成状态
                SyncGroupJob syncGroupJob = syncGroupJobMapper.selectByDeviceUniqueIdAndStatus(device.getDeviceUniqueId(), 1);
                if (syncGroupJob != null) {
                    syncGroupJob.setStatus(2);
                    syncGroupJob.setUpdateTime(now);
                    syncGroupJobMapper.updateByIdSelective(syncGroupJob);

                    log.info("--监控系统日志--更新群成员状态--");
                }
            }

        }catch (Exception e){
            log.error(e.getMessage(), e);
        }finally {
            log.info("--监控系统日志--结束--");
        }

    }

}
